#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

int var =0;

void *thmain1(void *arg);   // 线程1的主函数

int main(int argc, char *argv[])
{
    pthread_t thid1 = 0;

    if (pthread_create(&thid1, NULL, thmain1, NULL) != 0)
    {
        printf("pthread_create failed.\n");
        exit(-1);
    }

    usleep(100);
    pthread_cancel(thid1);

    void *ret;
    int result;
    // 等待子线程退出。
    printf("join...\n");
    result = pthread_join(thid1, &ret);
    printf("thid1 result=%d,ret=%ld\n", result, ret);
    printf("join ok.\n");

    printf("var=%d\n", var);
}

void *thmain1(void *arg)
{
    // pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);

    for(var = 0; var < 400000000; ++var){
        pthread_testcancel();
    }
    return (void*)1;
}
